from django.db import models, transaction
from django.db.models import F

from social.account.models import User
from social.payment.models import Wallet, WalletRecord
from social.rank.tasks import add_gift_record_rank
from social.utils.models import BaseModel
from social.gift.tasks import send_gift_message


class Gift(BaseModel):
    ACTIVE_GIFT = [0, 1, 2]
    # 0：普通礼物，2：vip礼物，1:全局礼物，3:搭讪礼物，10：已下架
    thumb = models.CharField(max_length=100)
    coin = models.IntegerField(default=0)
    name = models.CharField(max_length=20)
    type = models.IntegerField(default=0)
    sort = models.IntegerField(default=0)

    def send(self, from_user, to_user):
        from social.user.models import UserLevel
        with transaction.atomic():
            gift_record = GiftRecord.objects.create(from_user=from_user, to_user=to_user, gift=self)
            # 搭讪礼物是不计算钻石消费的
            if self.type != 3:
                wallet1, _ = Wallet.objects.get_or_create(user=from_user)
                wallet1.consume(self.coin)

            # 财富值
            user_level, _ = UserLevel.objects.get_or_create(user=from_user)
            user_level.wealth_value = F('wealth_value') + self.coin
            user_level.save(update_fields=['wealth_value'])

            WalletRecord.objects.create(user=from_user, title=f'送给 {to_user.nickname} {self.name}（增加{self.coin}财富值）',
                                        type=WalletRecord.COIN, change=-self.coin)

            wallet2, _ = Wallet.objects.get_or_create(user=to_user)
            wallet2.receive_gift(self)

            WalletRecord.objects.create(user=to_user, title=f'收到 {from_user.nickname} {self.name}',
                                        type=WalletRecord.INCOME,
                                        change=int(self.coin * wallet2.share_ratio / 10.0))

            # 魅力值
            user_level, _ = UserLevel.objects.get_or_create(user=to_user)
            user_level.charm_value = F('charm_value') + self.coin
            user_level.save(update_fields=['charm_value'])

        send_gift_message.delay(gift_record.id)
        add_gift_record_rank.delay(gift_record.id)


class GiftRecord(BaseModel):
    gift = models.ForeignKey(Gift, related_name='gift_records', on_delete=models.CASCADE)
    from_user = models.ForeignKey(User, related_name='send_gift_records', on_delete=models.CASCADE)
    to_user = models.ForeignKey(User, related_name='to_gift_records', on_delete=models.CASCADE)
    is_ranked = models.BooleanField(default=False)
    count = models.IntegerField(default=1)


def init_gifts():
    gs = Gift.objects.all()
    for x in gs:
        x.delete()
    Gift.objects.create(thumb='/gift/xiaoxingxing.png', coin=4, name='小星星')
    Gift.objects.create(thumb='/gift/yingguangbang.png', coin=12, name='荧光棒')
    Gift.objects.create(thumb='/gift/dengpai.png', coin=20, name='灯牌')
    Gift.objects.create(thumb='/gift/dahuanggua.png', coin=30, name='大黄瓜')
    Gift.objects.create(thumb='/gift/yuanliangmao.png', coin=60, name='原谅帽')
    Gift.objects.create(thumb='/gift/ruanmengmofa.png', coin=88, name='软萌魔法')
    Gift.objects.create(thumb='/gift/xiannvbang.png', coin=99, name='仙女棒')
    Gift.objects.create(thumb='/gift/love.png', coin=128, name='LOVE')
    Gift.objects.create(thumb='/gift/xiaciyiding.png', coin=266, name='下次一定')
    Gift.objects.create(thumb='/gift/mianhuatang.png', coin=333, name='棉花糖')
    Gift.objects.create(thumb='/gift/zuanjie.png', coin=520, name='钻戒')
    Gift.objects.create(thumb='/gift/chenbaodanmu.png', coin=592, name='承包弹幕')
    Gift.objects.create(thumb='/gift/fenhongzhuazhua.png', coin=660, name='分红爪爪')
    Gift.objects.create(thumb='/gift/shengridaogao.png', coin=888, name='生日蛋糕')
    Gift.objects.create(thumb='/gift/youqianrenxin.png', coin=999, name='有钱任性')
    Gift.objects.create(thumb='/gift/tianmibaoji.png', coin=1200, name='甜蜜暴击')
    Gift.objects.create(thumb='/gift/qintianwawa.png', coin=1580, name='晴天娃娃')

    Gift.objects.create(thumb='/gift/chunvzuo.png', coin=1880, name='处女座', type=1)
    Gift.objects.create(thumb='/gift/haohuapaoche.png', coin=2000, name='豪华跑车', type=1)
    Gift.objects.create(thumb='/gift/fushichuying.png', coin=2990, name='富士春樱', type=2)
    Gift.objects.create(thumb='/gift/meiguilvcheng.png', coin=3680, name='玫瑰旅程', type=1)
    Gift.objects.create(thumb='/gift/tianezilian.png', coin=5000, name='天鹅之恋', type=2)
    Gift.objects.create(thumb='/gift/qimengxinguang.png', coin=6880, name='绮梦星光', type=2)
    Gift.objects.create(thumb='/gift/jingzhimeng.png', coin=8880, name='鲸之梦', type=1)

    Gift.objects.create(thumb='/gift/aidegongyang.png', coin=13140, name='爱的供养', type=1)
    Gift.objects.create(thumb='/gift/langmanchenbao.png', coin=18880, name='浪漫城堡', type=1)
    Gift.objects.create(thumb='/gift/youtingpaidui.png', coin=28880, name='游艇派对', type=1)
    Gift.objects.create(thumb='/gift/benfuyinhe.png', coin=30000, name='奔赴银河', type=2)
    Gift.objects.create(thumb='/gift/jingluoxinhai.png', coin=33440, name='鲸落星海', type=1)
    Gift.objects.create(thumb='/gift/feinimoshu.png', coin=52000, name='非你莫属', type=1)
    Gift.objects.create(thumb='/gift/yuzhoufeichuan.png', coin=88880, name='宇宙飞船', type=1)

    Gift.objects.create(thumb='/gift/bixin.png', coin=2, name='比心', type=3)
